using System;
using System.Collections.Generic;
using Infragistics.UltraChart.Resources.Appearance;
using PluginFrame;

namespace PcrExperimentProject
{
	[Serializable]
	public class GeneScanPcrExperimentProject : AbsolutePcrExperimentProject, IGeneScanPcrExperimentProject
	{
		[NonSerialized]
		protected IPcrCell mReferenceCell;

		[NonSerialized]
		protected int mMaxCountGroupIndex;

		[NonSerialized]
		protected List<IPcrCell>[] mResultHRMPcrCells;

		protected HRMModeEnum mHRMMode;

		protected bool mIsManualReference;

		protected double mSettingRate;

		public List<IPcrCell>[] ResultHRMPcrCells
		{
			get
			{
				return mResultHRMPcrCells;
			}
			set
			{
				mResultHRMPcrCells = value;
			}
		}

		public int MaxCountGroupIndex
		{
			get
			{
				return mMaxCountGroupIndex;
			}
			set
			{
				mMaxCountGroupIndex = value;
			}
		}

		public IPcrCell ReferenceCell
		{
			get
			{
				return mReferenceCell;
			}
			set
			{
				mReferenceCell = value;
			}
		}

		public bool IsManualReference
		{
			get
			{
				return mIsManualReference;
			}
			set
			{
				mIsManualReference = value;
			}
		}

		public double SettingRate
		{
			get
			{
				return mSettingRate;
			}
			set
			{
				mSettingRate = value;
			}
		}

		public HRMModeEnum HRMMode
		{
			get
			{
				return mHRMMode;
			}
			set
			{
				mHRMMode = value;
			}
		}

		public GeneScanPcrExperimentProject(IApplication app, List<bool> isChannelUsed)
			: this(app)
		{
		}

		public GeneScanPcrExperimentProject(IApplication app)
			: base(app)
		{
			mHRMMode = HRMModeEnum.Continue;
			mSettingRate = 0.02;
			mSystemType = PcrSystemType.GeneScan;
			mPcrProgram.HRMSegments[0].IsAvailable = true;
			mPcrProgram.HRMSegments[0].StartDegree = mConfigurationService.PcrProgramConfiguration.DefaultHRMStartTemperature;
			mPcrProgram.HRMSegments[0].StopDegree = mConfigurationService.PcrProgramConfiguration.DefaultHRMStopTemperature;
			mPcrProgram.HRMSegments[0].StepInterval = 0.1;
			mPcrProgram.HRMSegments[0].HoldTime = mConfigurationService.HRMHoldTime;
			mPcrProgram.RefreshHRMDetectionCount();
		}

		public void GenerateHRMDiffCurve()
		{
			string value = mConfigurationService.GetDetectionTypeArray(PcrSystemType.GeneScan)[0];
			int hRMChannelIndex = GetHRMChannelIndex();
			int position = mProjectItems[hRMChannelIndex].ChannelConfiguration.Position;
			if (mResultHRMPcrCells == null || mResultHRMPcrCells.Length == 0)
			{
				return;
			}
			IPcrCell pcrCell = null;
			int num = mMaxCountGroupIndex;
			if (mIsManualReference)
			{
				pcrCell = mReferenceCell;
			}
			if (pcrCell == null)
			{
				double num2 = 0.0;
				double num3 = 0.0;
				IPcrCell pcrCell2 = null;
				IPcrCell pcrCell3 = null;
				foreach (IPcrCell item in mResultHRMPcrCells[num])
				{
					double num4 = Convert.ToDouble(item.GetCellInformation("QualityKey", position));
					string cellInformation = item.GetCellInformation("DetectionTypeKey", position);
					if (cellInformation.Equals(value))
					{
						if (num4 > num2)
						{
							num2 = num4;
							pcrCell2 = item;
						}
					}
					else if (num4 > num3)
					{
						num3 = num4;
						pcrCell3 = item;
					}
				}
				pcrCell = pcrCell2 ?? pcrCell3;
				mIsManualReference = false;
				mReferenceCell = pcrCell;
			}
			if (pcrCell == null)
			{
				return;
			}
			XYSeries hRMNormalizedCurveSeries = pcrCell.GetHRMNormalizedCurveSeries();
			for (int i = 0; i < mResultHRMPcrCells.Length; i++)
			{
				double num5 = double.PositiveInfinity;
				double num6 = double.NegativeInfinity;
				foreach (IPcrCell item2 in mResultHRMPcrCells[i])
				{
					XYSeries hRMNormalizedCurveSeries2 = item2.GetHRMNormalizedCurveSeries();
					XYSeries hRMDiffCurveSeries = item2.GetHRMDiffCurveSeries();
					hRMDiffCurveSeries.Points.Clear();
					int num7 = ((hRMNormalizedCurveSeries.Points.Count > hRMNormalizedCurveSeries2.Points.Count) ? hRMNormalizedCurveSeries2.Points.Count : hRMNormalizedCurveSeries.Points.Count);
					int num8 = 10;
					for (int j = 0; j < num7; j++)
					{
						if (num8 < 10)
						{
							num8++;
							continue;
						}
						num8 = 1;
						double num9 = Math.Round(hRMNormalizedCurveSeries.Points[j].ValueY - hRMNormalizedCurveSeries2.Points[j].ValueY, 3);
						double valueX = Math.Round(hRMNormalizedCurveSeries2.Points[j].ValueX, 3);
						XYDataPoint point = new XYDataPoint(valueX, num9, hRMNormalizedCurveSeries2.Points[j].Label, false);
						hRMDiffCurveSeries.Points.Add(point);
						if (num9 > num6)
						{
							num6 = num9;
						}
						if (num9 < num5)
						{
							num5 = num9;
						}
					}
					double valueY = Math.Round(hRMNormalizedCurveSeries.Points[num7 - 1].ValueY - hRMNormalizedCurveSeries2.Points[num7 - 1].ValueY, 3);
					double valueX2 = Math.Round(hRMNormalizedCurveSeries2.Points[num7 - 1].ValueX, 3);
					XYDataPoint point2 = new XYDataPoint(valueX2, valueY, hRMNormalizedCurveSeries2.Points[num7 - 1].Label, false);
					hRMDiffCurveSeries.Points.Add(point2);
					item2.HRMDiffCurveMax = num6;
					item2.HRMDiffCurveMin = num5;
				}
			}
		}

		public int GetHRMChannelIndex()
		{
			int result = 0;
			for (int i = 0; i < mProjectItems.Length; i++)
			{
				if (mProjectItems[i] != null && mProjectItems[i].ChannelConfiguration.IsSelected)
				{
					result = i;
					break;
				}
			}
			return result;
		}

		protected override IAnalysisParameterConfiguration CreateMeltingAnalysisParameterConfiguration(string name, int targetIndex)
		{
			return mConfigurationService.CreateHRMAnalysisParameterConfiguration(name, targetIndex, this) as IAnalysisParameterConfiguration;
		}
	}
}
